package com.zhong.beautifulimageapp.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.zhong.beautifulimageapp.R;
import com.zhong.beautifulimageapp.model.TopicDetail;
import com.zhong.beautifulimageapp.utils.ChangeTheHtmlImg;
import com.zhong.beautifulimageapp.utils.DateTimeDistance;
import com.zhong.beautifulimageapp.utils.WebViewContentStyle;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 主题回复的适配器
 * Created by 俊峰 on 2017/7/18.
 */

public class TopicListViewAdapter extends BaseAdapter {

    private List<TopicDetail.DataBean.RepliesBean> mRepliesBeanList = new ArrayList<>();
    private LayoutInflater mLayoutInflater;
    private Context mContext;
    private Map<String, Integer> replayList = new HashMap<>();

    public TopicListViewAdapter(Context context) {
        mContext = context;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    public void setReplayList(List<TopicDetail.DataBean.RepliesBean> repliesBeanList){
        this.mRepliesBeanList.clear();
        this.mRepliesBeanList.addAll(repliesBeanList);
        replayList.clear();
        for (int i = 0; i < repliesBeanList.size(); i++) {
            replayList.put(repliesBeanList.get(i).getId(), i + 1);
        }
    }

    @Override
    public int getCount() {
        int result = 0;
        if (mRepliesBeanList != null) {
            result = mRepliesBeanList.size();
        }
        return result;
    }

    @Override
    public Object getItem(int position) {
        return mRepliesBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        if (convertView != null) {
            view = convertView;
        } else {
            view = mLayoutInflater.inflate(R.layout.item_replay, parent, false);
        }
        ViewHolder viewHolder = (ViewHolder) view.getTag();
        if (viewHolder == null){
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        }
        int floorHeight = position + 1;
        onBindData(viewHolder, mRepliesBeanList.get(position), floorHeight);
        return view;
    }

    private void onBindData(final ViewHolder holder, TopicDetail.DataBean.RepliesBean bean, int height) {
        Glide.with(mContext)
                .load(bean.getAuthor().getAvatar_url())
                .asBitmap()
                .placeholder(R.mipmap.app)
                .centerCrop()
                .into(new BitmapImageViewTarget(holder.mTopicDetailItemReplayHeaderUserIcon){
                    @Override
                    protected void setResource(Bitmap resource) {
                        RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(mContext.getResources(), resource);
                        roundedBitmapDrawable.setCircular(true);
                        holder.mTopicDetailItemReplayHeaderUserIcon.setImageDrawable(roundedBitmapDrawable);
                    }
                });
        String mFloorHeight = height + "楼";
        holder.mTopicDetailItemReplayHeaderClickUpCount.setText(String.valueOf(bean.getUps().size()));
        holder.mTopicDetailItemReplayHeaderUserName.setText(bean.getAuthor().getLoginname());
        holder.mTopicDetailItemReplayHeaderTopicCreateTime.setText(mFloorHeight);
        holder.mTopicDetailItemReplayHeaderTimeDistance.setText(DateTimeDistance.getTimeDistance(bean.getCreate_at(), 2));
        if (bean.getReply_id() == null){
            holder.mTopicDetailItemReplayHeaderReplayTitle.setVisibility(View.GONE);
        }else {
            String mReplayFloorHeight = "回复：" + replayList.get(String.valueOf(bean.getReply_id()));
            holder.mTopicDetailItemReplayHeaderReplayTitle.setVisibility(View.VISIBLE);
            holder.mTopicDetailItemReplayHeaderReplayTitle.setText(mReplayFloorHeight);
        }
        holder.mTopicDetailItemReplayHeaderBelowContent.setWebChromeClient(new WebChromeClient());
        holder.mTopicDetailItemReplayHeaderBelowContent.setWebViewClient(new WebViewClient());
        WebSettings settings = holder.mTopicDetailItemReplayHeaderBelowContent.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        settings.setSupportZoom(false);
        settings.setAllowFileAccess(true);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        String data = WebViewContentStyle.HTML_0 + WebViewContentStyle.LIGHT_THEME_CSS
                + WebViewContentStyle.HTML_1 + ChangeTheHtmlImg.changeImgSize(bean.getContent())
                + "\n"
                + WebViewContentStyle.HTML_2;
        holder.mTopicDetailItemReplayHeaderBelowContent.loadDataWithBaseURL(null, data, "text/html;charset=UTF-8", null, null);
    }

    static class ViewHolder {
        @BindView(R.id.topic_detail_item_replay_header_user_icon)
        ImageView mTopicDetailItemReplayHeaderUserIcon;
        @BindView(R.id.topic_detail_item_replay_header_click_up_img)
        ImageView mTopicDetailItemReplayHeaderClickUpImg;
        @BindView(R.id.topic_detail_item_replay_header_click_up_count)
        TextView mTopicDetailItemReplayHeaderClickUpCount;
        @BindView(R.id.topic_detail_item_replay_header_click_up_all)
        RelativeLayout mTopicDetailItemReplayHeaderClickUpAll;
        @BindView(R.id.topic_detail_item_replay_header_click_replay)
        ImageView mTopicDetailItemReplayHeaderClickReplay;
        @BindView(R.id.topic_detail_item_replay_header_user_name)
        TextView mTopicDetailItemReplayHeaderUserName;
        @BindView(R.id.topic_detail_item_replay_header_topicCreateTime)
        TextView mTopicDetailItemReplayHeaderTopicCreateTime;
        @BindView(R.id.topic_detail_item_replay_header_timeDistance)
        TextView mTopicDetailItemReplayHeaderTimeDistance;
        @BindView(R.id.topic_detail_item_replay_header_replay_title)
        TextView mTopicDetailItemReplayHeaderReplayTitle;
        @BindView(R.id.topic_detail_item_replay_header_below_content)
        WebView mTopicDetailItemReplayHeaderBelowContent;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
